//
//  UIViewController+YFBaseVcCategory.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/3.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "UIViewController+YFBaseVcCategory.h"
#import <objc/runtime.h>
#import "YFMMDrawerStateManager.h"

@implementation UIViewController (YFBaseVcCategory)

+ (NSString *)defalutIndentify {
    return NSStringFromClass([self class]);
}

#pragma mark - 设置导航栏
- (void)setBaseBackBarItem {
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_back"]  style:UIBarButtonItemStyleDone target:self action:@selector(baseBackBarItemAction:)];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)baseBackBarItemAction:(UIBarButtonItem *)item {
    if ([self.yfBaseDelegate respondsToSelector:@selector(customBaseBackNavBarItemAction:)]) {
        [self.yfBaseDelegate customBaseBackNavBarItemAction:item];
    }else {
        if (self.presentingViewController) {
            [self dismissViewControllerAnimated:YES completion:nil];
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (UIBarButtonItem *)addNavBarItemWithTitle:(NSString *)title orImageName:(UIImage *)image action:(void(^)(UIBarButtonItem *))barItemAction {
    UIBarButtonItem *barItem = nil;
    if (image) {
        barItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(navBarItemAction:)];
    }else {
        barItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(navBarItemAction:)];
    }
    barItem.navBarBlock = barItemAction;
    return barItem;
}

- (void)navBarItemAction:(UIBarButtonItem *)barItem {
    if (barItem.navBarBlock) {
        barItem.navBarBlock(barItem);
    }
}

- (void)setMenuNavBar {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(leftNavItemClick)];
}

- (void)leftNavItemClick {
    [[YFMMDrawerStateManager sharedManager].drawerController openDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

//是否自动旋转,返回YES可以自动旋转,返回NO禁止旋转
- (BOOL)shouldAutorotate{
    return NO;
}
//返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}
//由模态推出的视图控制器 优先支持的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}
- (void)setYfBaseDelegate:(id<UIViewControllerBaseDelegate>)yfBaseDelegate {
     objc_setAssociatedObject(self, @"yfBaseDelegate", yfBaseDelegate, OBJC_ASSOCIATION_ASSIGN);
}

- (id<UIViewControllerBaseDelegate>)yfBaseDelegate {
   return objc_getAssociatedObject(self, @"yfBaseDelegate");
}
- (void)setCanLeftOrRightGesOpenMenuView:(BOOL)canLeftOrRightGesOpenMenuView {
    objc_setAssociatedObject(self, @"canLeftOrRightGesOpenMenuView", @(canLeftOrRightGesOpenMenuView), OBJC_ASSOCIATION_ASSIGN);
}
- (BOOL)canLeftOrRightGesOpenMenuView {
    return objc_getAssociatedObject(self, @"canLeftOrRightGesOpenMenuView");
}




@end

@implementation UIBarButtonItem (YFBarItem)

- (void)setNavBarBlock:(NavBarItemActionBlock)navBarBlock {
    /*
      OBJC_ASSOCIATION_COPY = 01403 //关联对象的属性是copy并且关联对象使用原子性
     */
    objc_setAssociatedObject(self, @"navBarBlock", navBarBlock, OBJC_ASSOCIATION_COPY);
}

- (NavBarItemActionBlock)navBarBlock {
    return objc_getAssociatedObject(self, @"navBarBlock");
}

@end
